﻿#region Using Directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.Design;

using PAT.Entities;
using PAT.Data;
using PAT.Data.Bases;
using PAT.Services;
#endregion

namespace PAT.Web.Data
{
	/// <summary>
	/// Represents the DataRepository.PatTrabajoOrdenProvider object that provides
	/// data to data-bound controls in multi-tier Web application architectures.
	/// </summary>
	[Designer(typeof(PatTrabajoOrdenDataSourceDesigner))]
	public class PatTrabajoOrdenDataSource : ProviderDataSource<PatTrabajoOrden, PatTrabajoOrdenKey>
	{
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatTrabajoOrdenDataSource class.
		/// </summary>
		public PatTrabajoOrdenDataSource() : base(new PatTrabajoOrdenService())
		{
		}

		#endregion Constructors
		
		#region Properties
		
		/// <summary>
		/// Gets a reference to the PatTrabajoOrdenDataSourceView used by the PatTrabajoOrdenDataSource.
		/// </summary>
		protected PatTrabajoOrdenDataSourceView PatTrabajoOrdenView
		{
			get { return ( View as PatTrabajoOrdenDataSourceView ); }
		}
		
		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the PatTrabajoOrdenDataSource control invokes to retrieve data.
		/// </summary>
		public PatTrabajoOrdenSelectMethod SelectMethod
		{
			get
			{
				PatTrabajoOrdenSelectMethod selectMethod = PatTrabajoOrdenSelectMethod.GetAll;
				Object method = ViewState["SelectMethod"];
				if ( method != null )
				{
					selectMethod = (PatTrabajoOrdenSelectMethod) method;
				}
				return selectMethod;
			}
			set { ViewState["SelectMethod"] = value; }
		}

		#endregion Properties
		
		#region Methods

		/// <summary>
		/// Creates a new instance of the PatTrabajoOrdenDataSourceView class that is to be
		/// used by the PatTrabajoOrdenDataSource.
		/// </summary>
		/// <returns>An instance of the PatTrabajoOrdenDataSourceView class.</returns>
		protected override BaseDataSourceView<PatTrabajoOrden, PatTrabajoOrdenKey> GetNewDataSourceView()
		{
			return new PatTrabajoOrdenDataSourceView(this, DefaultViewName);
		}
		
		/// <summary>
        /// Creates a cache hashing key based on the startIndex, pageSize and the SelectMethod being used.
        /// </summary>
        /// <param name="startIndex">The current start row index.</param>
        /// <param name="pageSize">The current page size.</param>
        /// <returns>A string that can be used as a key for caching purposes.</returns>
		protected override string CacheHashKey(int startIndex, int pageSize)
        {
			return String.Format("{0}:{1}:{2}", SelectMethod, startIndex, pageSize);
        }
		
		#endregion Methods
	}
	
	/// <summary>
	/// Supports the PatTrabajoOrdenDataSource control and provides an interface for
	/// data-bound controls to perform data operations with business and data objects.
	/// </summary>
	public class PatTrabajoOrdenDataSourceView : ProviderDataSourceView<PatTrabajoOrden, PatTrabajoOrdenKey>
	{
		#region Declarations

		#endregion Declarations
		
		#region Constructors

		/// <summary>
		/// Initializes a new instance of the PatTrabajoOrdenDataSourceView class.
		/// </summary>
		/// <param name="owner">A reference to the PatTrabajoOrdenDataSource which created this instance.</param>
		/// <param name="viewName">The name of the view.</param>
		public PatTrabajoOrdenDataSourceView(PatTrabajoOrdenDataSource owner, String viewName)
			: base(owner, viewName)
		{
		}
		
		#endregion Constructors
		
		#region Properties

		/// <summary>
		/// Gets a strongly-typed reference to the Owner property.
		/// </summary>
		internal PatTrabajoOrdenDataSource PatTrabajoOrdenOwner
		{
			get { return Owner as PatTrabajoOrdenDataSource; }
		}

		/// <summary>
		/// Gets or sets the name of the method or function that
		/// the DataSource control invokes to retrieve data.
		/// </summary>
		internal PatTrabajoOrdenSelectMethod SelectMethod
		{
			get { return PatTrabajoOrdenOwner.SelectMethod; }
			set { PatTrabajoOrdenOwner.SelectMethod = value; }
		}

		/// <summary>
		/// Gets a strongly typed reference to the Provider property.
		/// </summary>
		internal PatTrabajoOrdenService PatTrabajoOrdenProvider
		{
			get { return Provider as PatTrabajoOrdenService; }
		}

		#endregion Properties
		
		#region Methods
		
		/// <summary>
		/// Gets a collection of Entity objects based on the value of the SelectMethod property.
		/// </summary>
		/// <param name="count">The total number of rows in the DataSource.</param>
		/// <returns>A collection of Entity objects.</returns>
		protected override IList<PatTrabajoOrden> GetSelectData(out int count)
		{
			Hashtable values = CollectionsUtil.CreateCaseInsensitiveHashtable(GetParameterValues());
			Hashtable customOutput = CollectionsUtil.CreateCaseInsensitiveHashtable();
			IList<PatTrabajoOrden> results = null;
			PatTrabajoOrden item;
			count = 0;
			
			System.Byte _idTrabajo;
			System.Int32 _idTaller;

			switch ( SelectMethod )
			{
				case PatTrabajoOrdenSelectMethod.Get:
					PatTrabajoOrdenKey entityKey  = new PatTrabajoOrdenKey();
					entityKey.Load(values);
					item = PatTrabajoOrdenProvider.Get(entityKey);
					results = new TList<PatTrabajoOrden>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				case PatTrabajoOrdenSelectMethod.GetAll:
                    results = PatTrabajoOrdenProvider.GetAll(StartIndex, PageSize, out count);
                    break;
				case PatTrabajoOrdenSelectMethod.GetPaged:
					results = PatTrabajoOrdenProvider.GetPaged(WhereClause, OrderBy, PageIndex, PageSize, out count);
					break;
				case PatTrabajoOrdenSelectMethod.Find:
					if ( FilterParameters != null )
						results = PatTrabajoOrdenProvider.Find(FilterParameters, OrderBy, StartIndex, PageSize, out count);
					else
						results = PatTrabajoOrdenProvider.Find(WhereClause, StartIndex, PageSize, out count);
                    break;
				// PK
				case PatTrabajoOrdenSelectMethod.GetByIdTrabajo:
					_idTrabajo = ( values["IdTrabajo"] != null ) ? (System.Byte) EntityUtil.ChangeType(values["IdTrabajo"], typeof(System.Byte)) : (byte)0;
					item = PatTrabajoOrdenProvider.GetByIdTrabajo(_idTrabajo);
					results = new TList<PatTrabajoOrden>();
					if ( item != null ) results.Add(item);
					count = results.Count;
					break;
				// IX
				// FK
				// M:M
				case PatTrabajoOrdenSelectMethod.GetByIdTallerFromPatCapacidadTaller:
					_idTaller = ( values["IdTaller"] != null ) ? (System.Int32) EntityUtil.ChangeType(values["IdTaller"], typeof(System.Int32)) : (int)0;
					results = PatTrabajoOrdenProvider.GetByIdTallerFromPatCapacidadTaller(_idTaller, this.StartIndex, this.PageSize, out count);
					break;
				// Custom
				default:
					break;
			}

			if ( results != null && count < 1 )
			{
				count = results.Count;

				if ( !String.IsNullOrEmpty(CustomMethodRecordCountParamName) )
				{
					object objCustomCount = EntityUtil.ChangeType(customOutput[CustomMethodRecordCountParamName], typeof(Int32));
					
					if ( objCustomCount != null )
					{
						count = (int) objCustomCount;
					}
				}
			}
			
			return results;
		}
		
		/// <summary>
		/// Gets the values of any supplied parameters for internal caching.
		/// </summary>
		/// <param name="values">An IDictionary object of name/value pairs.</param>
		protected override void GetSelectParameters(IDictionary values)
		{
			if ( SelectMethod == PatTrabajoOrdenSelectMethod.Get || SelectMethod == PatTrabajoOrdenSelectMethod.GetByIdTrabajo )
			{
				EntityId = GetEntityKey(values);
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation for the current entity if it has
		/// not already been performed.
		/// </summary>
		internal override void DeepLoad()
		{
			if ( !IsDeepLoaded )
			{
				PatTrabajoOrden entity = GetCurrentEntity();
				
				if ( entity != null )
				{
					// init transaction manager
					GetTransactionManager();
					// execute deep load method
					PatTrabajoOrdenProvider.DeepLoad(GetCurrentEntity(), EnableRecursiveDeepLoad);
					// set loaded flag
					IsDeepLoaded = true;
				}
			}
		}

		/// <summary>
		/// Performs a DeepLoad operation on the specified entity collection.
		/// </summary>
		/// <param name="entityList"></param>
		/// <param name="properties"></param>
		internal override void DeepLoad(TList<PatTrabajoOrden> entityList, ProviderDataSourceDeepLoadList properties)
		{
			// init transaction manager
			GetTransactionManager();
			// execute deep load method
			PatTrabajoOrdenProvider.DeepLoad(entityList, properties.Recursive, properties.Method, properties.GetTypes());
		}

		#endregion Select Methods
	}
	
	#region PatTrabajoOrdenDataSourceDesigner

	/// <summary>
	/// Provides design-time support in a design host for the PatTrabajoOrdenDataSource class.
	/// </summary>
	public class PatTrabajoOrdenDataSourceDesigner : ProviderDataSourceDesigner<PatTrabajoOrden, PatTrabajoOrdenKey>
	{
		/// <summary>
		/// Initializes a new instance of the PatTrabajoOrdenDataSourceDesigner class.
		/// </summary>
		public PatTrabajoOrdenDataSourceDesigner()
		{
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public PatTrabajoOrdenSelectMethod SelectMethod
		{
			get { return ((PatTrabajoOrdenDataSource) DataSource).SelectMethod; }
			set { SetPropertyValue("SelectMethod", value); }
		}

		/// <summary>Gets the designer action list collection for this designer.</summary>
		/// <returns>The <see cref="T:System.ComponentModel.Design.DesignerActionListCollection"/>
		/// associated with this designer.</returns>
		public override DesignerActionListCollection ActionLists
		{
			get
			{
				DesignerActionListCollection actions = new DesignerActionListCollection();
				actions.Add(new PatTrabajoOrdenDataSourceActionList(this));
				actions.AddRange(base.ActionLists);
				return actions;
			}
		}
	}

	#region PatTrabajoOrdenDataSourceActionList

	/// <summary>
	/// Supports the PatTrabajoOrdenDataSourceDesigner class.
	/// </summary>
	internal class PatTrabajoOrdenDataSourceActionList : DesignerActionList
	{
		private PatTrabajoOrdenDataSourceDesigner _designer;

		/// <summary>
		/// Initializes a new instance of the PatTrabajoOrdenDataSourceActionList class.
		/// </summary>
		/// <param name="designer"></param>
		public PatTrabajoOrdenDataSourceActionList(PatTrabajoOrdenDataSourceDesigner designer) : base(designer.Component)
		{
			_designer = designer;
		}

		/// <summary>
		/// Gets or sets the SelectMethod property.
		/// </summary>
		public PatTrabajoOrdenSelectMethod SelectMethod
		{
			get { return _designer.SelectMethod; }
			set { _designer.SelectMethod = value; }
		}

		/// <summary>
		/// Returns the collection of <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// objects contained in the list.
		/// </summary>
		/// <returns>A <see cref="T:System.ComponentModel.Design.DesignerActionItem"/>
		/// array that contains the items in this list.</returns>
		public override DesignerActionItemCollection GetSortedActionItems()
		{
			DesignerActionItemCollection items = new DesignerActionItemCollection();
			items.Add(new DesignerActionPropertyItem("SelectMethod", "Select Method", "Methods"));
			return items;
		}
	}

	#endregion PatTrabajoOrdenDataSourceActionList
	
	#endregion PatTrabajoOrdenDataSourceDesigner
	
	#region PatTrabajoOrdenSelectMethod
	
	/// <summary>
	/// Enumeration of method names available for the PatTrabajoOrdenDataSource.SelectMethod property.
	/// </summary>
	public enum PatTrabajoOrdenSelectMethod
	{
		/// <summary>
		/// Represents the Get method.
		/// </summary>
		Get,
		/// <summary>
		/// Represents the GetAll method.
		/// </summary>
		GetAll,
		/// <summary>
		/// Represents the GetPaged method.
		/// </summary>
		GetPaged,
		/// <summary>
		/// Represents the Find method.
		/// </summary>
		Find,
		/// <summary>
		/// Represents the GetByIdTrabajo method.
		/// </summary>
		GetByIdTrabajo,
		/// <summary>
		/// Represents the GetByIdTallerFromPatCapacidadTaller method.
		/// </summary>
		GetByIdTallerFromPatCapacidadTaller
	}
	
	#endregion PatTrabajoOrdenSelectMethod

	#region PatTrabajoOrdenFilter
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlFilter&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatTrabajoOrden"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatTrabajoOrdenFilter : SqlFilter<PatTrabajoOrdenColumn>
	{
	}
	
	#endregion PatTrabajoOrdenFilter

	#region PatTrabajoOrdenExpressionBuilder
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="SqlExpressionBuilder&lt;EntityColumn&gt;"/> class
	/// that is used exclusively with a <see cref="PatTrabajoOrden"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatTrabajoOrdenExpressionBuilder : SqlExpressionBuilder<PatTrabajoOrdenColumn>
	{
	}
	
	#endregion PatTrabajoOrdenExpressionBuilder	

	#region PatTrabajoOrdenProperty
	
	/// <summary>
	/// A strongly-typed instance of the <see cref="ChildEntityProperty&lt;PatTrabajoOrdenChildEntityTypes&gt;"/> class
	/// that is used exclusively with a <see cref="PatTrabajoOrden"/> object.
	/// </summary>
	[CLSCompliant(true)]
	public class PatTrabajoOrdenProperty : ChildEntityProperty<PatTrabajoOrdenChildEntityTypes>
	{
	}
	
	#endregion PatTrabajoOrdenProperty
}

